<?php
namespace app\controllers;


use app\logic\AdvLogic;
use app\common\corelib\LocalUpload;
use yii;

class AdvController extends BackendController
{

    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $advLogic = new AdvLogic();
        $this->data['breadcrumbs'] = '广告位管理';
        return $this->render('index', [
            'adv' => $advLogic->getAdv($query, $page),
            'query' => $query
        ]);
    }
    public function actionEdit()
    {
        $advLogic = new AdvLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('ad_id');
            $adv = $advLogic->getById($id);
            $this->data['breadcrumbs'] = ['广告管理' => U('adv/index'), $adv['title'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$adv['pic'])){
                $pic = $adv['pic'];
            }else{
                $pic = '/default_adv.jpg';
            }
            return $this->render('edit', [
                'adv' => $adv,
                'pic' => $pic,
                'theme' => '/assets/',
                'actionUrl' => U('adv/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('ad_id');
            if($_FILES['pic']['name']){
                LocalUpload::go_upload($_FILES['pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }

            $logicRes = $advLogic->editAdvById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新广告成功');
                jump('adv/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新广告失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }


    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('ad_id');
        $advLogic = new AdvLogic();
        $logicRes = $advLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改广告状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改广告状态失败');
        }
        return $this->redirect(U('adv/index'));
    }

    public function actionAdd()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['标签管理' => U('adv/index'), ' 添加广告' => ''];
            return $this->render('add', [
                'theme' => '/assets/',
                'actionUrl' => U('adv/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $advLogic = new AdvLogic();
            if($_FILES['pic']['name']){
                LocalUpload::go_upload($_FILES['pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $advLogic->addAdv($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增广告成功');
                jump('adv/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增广告失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }




}